﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ThreeByte.ActiveDeck.Data.UserResponse;
using ThreeByte.ActiveDeck.Config;
using log4net;

namespace ThreeByte.ActiveDeck.AddIn.Content.UserResponse
{
    /// <summary>
    /// Interaction logic for CategoriesDataUserControl2.xaml
    /// </summary>
    public partial class CategoriesDataUserControl2 : UserControl
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(CategoriesDataUserControl2));

        public CategoriesDataUserControl2() {
            log.Debug("Construct CategoriesDataUserControl2");
            InitializeComponent();
        }

        public CategoriesConfig Config { get; set; }


        public void Refresh(CategoriesUserData userData) {
            log.Debug("Refresh CategoriesDataUserControl2");
            LayoutRoot.Children.Clear();
            LayoutRoot.ColumnDefinitions.Clear();

            int currentColumn = -1;
            foreach(CategoryData cat in userData.Categories) {
                //Add a Column
                currentColumn++;

                LayoutRoot.ColumnDefinitions.Add(new ColumnDefinition());

                //Add the BackgroundBorder
                Border newBgBorder = new Border();
                newBgBorder.Margin = new Thickness(10);
                newBgBorder.SetValue(Grid.ColumnProperty, currentColumn);
                newBgBorder.SetValue(Grid.RowSpanProperty, 2);
                LayoutRoot.Children.Add(newBgBorder);

                //Get the category name from the slide config
                TextBlock categoryTitle = new TextBlock() { Text = Config.Categories[cat.ID - 1].Text };
                categoryTitle.Margin = new Thickness(20);
                categoryTitle.HorizontalAlignment = GetAlignmentForColumn(currentColumn);
                categoryTitle.SetValue(Grid.ColumnProperty, currentColumn);
                categoryTitle.SetValue(Grid.RowProperty, 0);
                LayoutRoot.Children.Add(categoryTitle);

                StackPanel newStack = new StackPanel();
                newStack.Margin = new Thickness(10);
                newStack.SetValue(Grid.ColumnProperty, currentColumn);
                newStack.SetValue(Grid.RowProperty, 1);
                LayoutRoot.Children.Add(newStack);


                //TODO: Make this more flexible
                for(int i = 0; i < 10 && i < cat.ItemValues.Count; i++) {
                    CategoryItemValue newValue = cat.ItemValues[i];
                    CategoriesItemElement2 newItem = new CategoriesItemElement2();
                    newItem.Word = Config.Words[newValue.ID - 1].Text;
                    newItem.Percent = newValue.Percent;
                    //newItem.Color = Brushes.Green;
                    //newItem.ColorInvert = Brushes.Red;

                    switch(currentColumn) {
                        case 0:
                            newItem.Color = new SolidColorBrush((Color)(ColorConverter.ConvertFromString("#FFF95100")));
                            newItem.ColorInvert = new SolidColorBrush((Color)(ColorConverter.ConvertFromString("#3FF95100")));
                            newItem.WordBox.HorizontalAlignment = HorizontalAlignment.Right;
                            newItem.PercentView2.Visibility = Visibility.Collapsed;
                            newItem.PercentBarLeft.Visibility = Visibility.Visible;
                            break;
                        case 1:
                            newItem.Color = new SolidColorBrush((Color)(ColorConverter.ConvertFromString("#FFFBFF00")));
                            newItem.ColorInvert = new SolidColorBrush((Color)(ColorConverter.ConvertFromString("#41DEE112")));
                            newItem.WordBox.HorizontalAlignment = HorizontalAlignment.Center;
                            newItem.PercentBarCenter.Visibility = Visibility.Visible;
                            break;
                        case 2:
                            newItem.Color = new SolidColorBrush((Color)(ColorConverter.ConvertFromString("#FF00FF16")));
                            newItem.ColorInvert = new SolidColorBrush((Color)(ColorConverter.ConvertFromString("#3F21A02C")));
                            newItem.WordBox.HorizontalAlignment = HorizontalAlignment.Left;
                            newItem.PercentView1.Visibility = Visibility.Collapsed;
                            newItem.PercentBarRight.Visibility = Visibility.Visible;
                            break;

                    }
                   
                    newStack.Children.Add(newItem);
                }

            }


        }

        private HorizontalAlignment GetAlignmentForColumn(int column) {
            log.DebugFormat("Get Alignment for column: {0}", column);
            switch(column) {
                case 0:
                    return HorizontalAlignment.Right;
                case 1:
                    return HorizontalAlignment.Center;
                case 2:
                    return HorizontalAlignment.Left;
                default:
                    return HorizontalAlignment.Center;
            }

        }

    }
}
